package com.gokzzz.controller;

import com.gokzzz.annotation.Log;
import com.gokzzz.dto.SysQuartzJobSaveOrUpdateDTO;
import com.gokzzz.dto.SysQuartzJobSearchQueryDTO;
import com.gokzzz.response.Response;
import com.gokzzz.service.ISysQuartzJobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

/**
 * @author ganfujia
 * @date 2022/4/22 16:02
 */
@RestController
@Api(tags = "系统：定时任务")
@RequiredArgsConstructor
@RequestMapping("/system/scheduler")
public class SysQuartzJobController {

    private final ISysQuartzJobService sysQuartzJobService;

    @Log("显示定时任务列表")
    @ApiOperation("显示定时任务列表")
    @PostMapping("/listJobByPage/{page}/{size}")
    public Response listJobByPage(@RequestBody SysQuartzJobSearchQueryDTO sysQuartzJobSearchQueryDTO,
                                  @PathVariable Long page,
                                  @PathVariable Long size) {

        return Response.success().setData(sysQuartzJobService.listJobByPage(sysQuartzJobSearchQueryDTO, page, size));
    }

    @Log("添加定时任务")
    @ApiOperation("添加定时任务")
    @PostMapping("/saveJob")
    public Response saveJob(@Validated({SysQuartzJobSaveOrUpdateDTO.Save.class}) @RequestBody
                                        SysQuartzJobSaveOrUpdateDTO sysQuartzJobSaveOrUpdateDTO) {
        sysQuartzJobService.saveJob(sysQuartzJobSaveOrUpdateDTO);
        return Response.success();
    }

    @Log("更新定时任务")
    @ApiOperation("更新定时任务")
    @PutMapping("/updateJob")
    public Response updateJob(@Validated({SysQuartzJobSaveOrUpdateDTO.Update.class}) @RequestBody
                                        SysQuartzJobSaveOrUpdateDTO sysQuartzJobSaveOrUpdateDTO) {
        sysQuartzJobService.updateJob(sysQuartzJobSaveOrUpdateDTO);
        return Response.success();
    }

    @Log("删除定时任务")
    @ApiOperation("删除定时任务")
    @DeleteMapping("/deleteJob")
    public Response deleteJob(@RequestBody Set<Long> ids){
        sysQuartzJobService.deleteJob(ids);
        return Response.success();
    }

    @Log("暂停定时任务")
    @ApiOperation("暂停定时任务")
    @PutMapping("/pauseJob/{id}")
    public Response pauseJob(@PathVariable Long id){
        sysQuartzJobService.pauseJob(id);
        return Response.success();
    }

    @Log("恢复定时任务")
    @ApiOperation("恢复定时任务")
    @PutMapping("/resumeJob/{id}")
    public Response resumeJob(@PathVariable Long id){
        sysQuartzJobService.resumeJob(id);
        return Response.success();
    }

    @Log("立即执行定时任务")
    @ApiOperation("立即执行定时任务")
    @PutMapping("/runJob/{id}")
    public Response runJob(@PathVariable Long id){
        sysQuartzJobService.runJob(id);
        return Response.success();
    }
}
